package com.weipeep.common.utils;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Strings;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.security.MessageDigest;
import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.*;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Util {
//  private static Logger log = LoggerFactory.getLogger(Util.class);

  private static ThreadLocal<ObjectMapper> objMapper = new ThreadLocal<ObjectMapper>() {
    @Override
    protected ObjectMapper initialValue() {
      ObjectMapper newInstance = new ObjectMapper();
      newInstance.setDateFormat(dateFormatLocal.get());
      return newInstance;
    }
  };
  private static final ThreadLocal<SimpleDateFormat> dateFormatGmt = new ThreadLocal<SimpleDateFormat>() {
    @Override
    protected SimpleDateFormat initialValue() {
      SimpleDateFormat newInstance = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
      newInstance.setTimeZone(TimeZone.getTimeZone("GMT"));
      return newInstance;
    }
  };
  private static final ThreadLocal<SimpleDateFormat> dateFormatLocal = new ThreadLocal<SimpleDateFormat>() {
      @Override
      protected SimpleDateFormat initialValue() {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      }
  };
  private static final ThreadLocal<SimpleDateFormat> df = new ThreadLocal<SimpleDateFormat>() {
    @Override
    protected SimpleDateFormat initialValue() {
      return new SimpleDateFormat("yyyy-MM-dd");
    }
  };
  private static final ThreadLocal<SimpleDateFormat> sdf = new ThreadLocal<SimpleDateFormat>() {
    @Override
    protected SimpleDateFormat initialValue() {
      return new SimpleDateFormat("yyyy.MM.dd");
    }
  };

  private static final DecimalFormat numFormat = new DecimalFormat("#0.00");

  private static String[] DATE_FROMAT = new String[]{ "yyyy", "yyyy-MM", "yyyy-MM-dd","yyyy.MM.dd","yyyy-MM-dd HH:mm", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm:ss.SSS" };
  private static String[] TIME_FORMAT = new String[] {"HH:mm", "HH:mm:ss"};

  public static final String REGEX_SCIENTIFIC_NOTATION = "^[1-9](\\.[0-9]+)?[eE][0-9]+$";

  public static String toJson(Object o) {
    try {
      return objMapper.get().writeValueAsString(o);
    } catch (JsonProcessingException e) {
      throw new RuntimeException(e);
    }
  }
  public static JSONObject toJsonObject(Object o) {
    try {
      String str = objMapper.get().writeValueAsString(o);
      return JSONObject.parseObject(str);
    } catch (JsonProcessingException e) {
      throw new RuntimeException(e);
    }
  }
  public static String formatTimestamp(Timestamp ts) {
    return dateFormatLocal.get().format(ts);
  }

  public static <T> T fromJson(String s, Class<T> clazz) {
    try {
      return objMapper.get().readValue(s, clazz);
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }
  @SuppressWarnings("rawtypes")
  public static <T> T fromJson(String s, TypeReference valueTypeRef) {
    try {
      return objMapper.get().readValue(s, valueTypeRef);
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }
}
